#include "tagger.h"

#include <QDebug>

Tagger::Tagger(QObject *parent) :
    QObject(parent)
{
    tagset = Corpus2::get_named_tagset("nkjp");
    tagging = false;
    taggingResult = NULL;
}

void Tagger::tagFile(const QString &fileName)
{
    tagging = true;
    delete taggingResult;
    taggingResult = new QTemporaryFile();
    taggingResult->open();
    QString program = "wcrft";
    QStringList arguments;
    arguments << "nkjp_s2.ini"
              << "-i" << "txt"
              << "-o" << "ccl"
              << "-d" << "/home/user/Pobrane/model_nkjp10_wcrft_s2"
              << fileName
              << "-O" << taggingResult->fileName();

    wcrft = new QProcess(this);
    connect(wcrft, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onWCRFTFinished(int,QProcess::ExitStatus)));
    wcrft->start(program, arguments);
}

QStringList Tagger::lexemQuery()
{
    QStringList result;
    cclReader = Corpus2::TokenReader::create_path_reader("ccl", tagset, taggingResult->fileName().toStdString());
    Corpus2::Token *token;
    while(token = cclReader->get_next_token()){
        std::string s = token->get_preferred_lexeme(tagset).lemma_utf8();
        result << QString::fromStdString(s);
    }
    return result;
}

void Tagger::onWCRFTFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    tagging = false;
    emit taggingCompleted(exitCode);
}
